import os
import numpy as np
import xlsxwriter as xlsx

files_dir = "./data_files"

# 获取文件夹下所有的符合条件的数据文件
all_files = os.listdir(files_dir)
log_files = []
for filename in all_files:
    if filename.startswith("runtime_data") and filename.endswith(".log"):
        log_files.append(filename)

# 读取指定的文件内容
strain_time = []
barrier_time = []
for filename in log_files:
    with open(files_dir + "/" + filename) as logfile:
        for line in logfile:
            line_words = line.split()
            if line_words[1] == "10":
                # 把文本转换成数值
                line_data = np.fromstring(line, sep=' ')
                strain_time.append(line_data[2])
                barrier_time.append(line_data[7])

# 将结果写入到Excel文件
workbook = xlsx.Workbook(files_dir + "/" + 'log_info.xlsx')
worksheet = workbook.add_worksheet()

# 调整列宽放文件名
worksheet.set_column('A:C', 20)

# 写入列名，加粗
bold_font = workbook.add_format({'bold': True})
worksheet.write('A1', 'Log File', bold_font)
worksheet.write('B1', 'Strain Time', bold_font)
worksheet.write('C1', 'Barrier Time', bold_font)

# 写入数据
nLine = len(log_files)
for i in range(nLine):
    row_number = i + 1
    worksheet.write(row_number, 0, log_files[i])
    worksheet.write(row_number, 1, strain_time[i])
    worksheet.write(row_number, 2, barrier_time[i])

# 关闭文件
workbook.close()
